package logic

import (
	"context"
	"fmt"
	"zhihu/models"

	"zhihu/zhihu_rpc/internal/svc"
	"zhihu/zhihu_rpc/zhihu_rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ReplyListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewReplyListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ReplyListLogic {
	return &ReplyListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ReplyListLogic) ReplyList(in *zhihu_rpc.ReplyListRequest) (*zhihu_rpc.ReplyListResponse, error) {
	// todo: add your logic here and delete this line
	var replies []struct {
		Content   string `gorm:"column:content"`
		UserName  string `gorm:"column:user_name"`
		UserImage string `gorm:"column:user_image"`
	}
	if models.DB.Model(&models.Reply{}).Where("trouble_id = ?", in.TroubleId).Joins("join users on replies.user_id = users.id").Select("replies.content as content,users.user_name as user_name , user_hand_image as user_image").Scan(&replies).Error != nil {
		return nil, fmt.Errorf("服务器繁忙")
	}
	var data []*zhihu_rpc.FromReplyList
	for _, reply := range replies {
		get := zhihu_rpc.FromReplyList{
			UserName:  reply.UserName,
			UserImage: reply.UserImage,
			Content:   reply.Content,
		}
		data = append(data, &get)
	}
	return &zhihu_rpc.ReplyListResponse{
		Replies: data,
	}, nil
}
